SHELL = @SHELL@

srcdir := @srcdir@
top_srcdir := @top_srcdir@
top_builddir := ..
subdir := exopc

VPATH = .:$(top_srcdir)/lib:$(top_srcdir)/$(subdir):@elements_vpath@$(top_srcdir)/elements/exopc

CROSS = ./
CC = $(CROSS)gcc
CXX = $(CROSS)g++
SETMAGIC=setmagic

#CC = @CC@
#CPP = @CPP@
#CXX = @CXX@
#CXXCPP = @CXXCPP@

.SUFFIXES:
.SUFFIXES: .S .c .cc .o .s

.c.o:
	$(COMPILE) -c $<
.s.o:
	$(COMPILE) -c $<
.S.o:
	$(COMPILE) -c $<
.cc.o:
	$(CXXCOMPILE) -c $<

GENERIC_OBJS = string.o straccum.o \
	bitvector.o hashmapi.o \
	integers.o ipaddress.o etheraddress.o packet.o \
	error.o glue.o timer.o \
	element.o unlimelement.o timedelement.o errorelement.o \
	confparse.o lexer.o elemfilter.o routervisitor.o router.o \
	crc32.o in_cksum.o iptable.o

EXOPC_OBJS = click.o syscall.o

-include elements.mk

OBJS = $(GENERIC_OBJS) $(ELEMENT_OBJS) $(EXOPC_OBJS) elements.o

CPPFLAGS = @CPPFLAGS@ -DCLICK_USERLEVEL
CFLAGS = @CFLAGS@
CXXFLAGS = @CXXFLAGS@ -fno-exceptions -fno-rtti


VOS=$(EXODEST)/vos
CRT0=$(VOS)/crt0_cpp.o
LIBOS=$(VOS)/libvos.a
LIBC=$(VOS)/libc.a
LIBGCC=$(VOS)/libgcc.a
INSTALLDIR=$(EXODEST)/home/benjie/ipb


DEFS = @DEFS@ -DEXOPC -DNOTEXOS
ifdef EXO_SMP
DEFS += -D__SMP__
endif


INCLUDES = -nostdinc -I$(EXODEST)/usr/include -I$(srcdir) -I$(top_srcdir) \
	-I$(top_srcdir)/lib
LDFLAGS = @LDFLAGS@
LIBS = @LIBS@

CXXCOMPILE = $(CXX) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CXXFLAGS)
CXXLD = $(CXX)
CXXLINK = $(CXXLD) $(CXXFLAGS) $(LDFLAGS) -o $@
COMPILE = $(CC) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS)
CCLD = $(CC)
LINK = $(CCLD) $(CFLAGS) $(LDFLAGS) -o $@

all: setup click ipb

setup:
	ln -sf cross-compile.sh g++
	ln -sf cross-compile.sh gcc

click: Makefile $(OBJS)
	$(CXX) -static -N -Ttext 800020 -nostdlib -o click $(CRT0) $(OBJS) $(LIBOS) $(LIBGCC) $(LIBC)
	cp click click.aout
	$(SETMAGIC) click 700
	mkdir -p $(INSTALLDIR)
	cp click $(INSTALLDIR)

ipb:
	cp samples/*.* $(INSTALLDIR)


Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
	cd $(top_builddir) \
	  && CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= $(SHELL) ./config.status

elemlist:
	@/bin/rm -f elements.conf
	@$(MAKE) elements.conf
elements.conf: $(top_builddir)/config.status $(top_builddir)/click-buildtool
	echo "exopc @element_groups@" | $(top_builddir)/click-buildtool findelem -r linuxmodule -p $(top_srcdir) > elements.conf
elements.mk: elements.conf $(top_builddir)/click-buildtool
	$(top_builddir)/click-buildtool elem2make -x "$(STD_ELEMENT_OBJS)" < elements.conf > elements.mk
elements.cc: elements.conf $(top_builddir)/click-buildtool
	$(top_builddir)/click-buildtool elem2export < elements.conf > elements.cc
	@rm -f elements.d

DEPFILES := $(wildcard *.d)
ifneq ($(DEPFILES),)
include $(DEPFILES)
endif

install install-man:

clean:
	rm -f *.d *.o click elements.mk elements.cc gcc g++ *.aout
distclean: clean
	-rm -f Makefile elements.conf

.PHONY: all clean distclean elemlist
